package com.gitee.ywj1352.算法.week06;

public class Demo {

    int ans = 0;

    public int reversePairs(int[] nums) {
        ms(nums, 0, nums.length - 1);
        return ans;
    }

    public void ms(int[] arr, int left, int right) {
        if (left >= right) return;
        int mid = (left + right) / 2;
        ms(arr, left, mid);
        ms(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }

    public void merge(int[] arr, int left, int mid, int right) {
        int[] temp = new int[right - left + 1];

        int l = left, m = mid + 1, i = 0;

        while (l <= mid && right >= m) {
            if (arr[l] > 2 * arr[m]) ans++;
            //temp[i++]
            temp[i++] = arr[l] >= arr[m] ? arr[m++] : arr[l++];
        }
        while (l <= mid) {
            temp[i++] = arr[l++];
        }
        while (right >= m) {
            temp[i++] = arr[m++];
        }
        for (int p = 0; p < temp.length; p++) {
            arr[left + p] = temp[p];
        }
    }

//    public static void merge2(int[] arr, int left, int mid, int right) {
//        int[] temp = new int[right - left + 1];
//        int i = left, j = mid + 1, k = 0;
//        while (i <= mid && j <= right) {
//            temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
//        }
//        while (i <= mid) temp[k++] = arr[i++];
//        while (j <= right) temp[k++] = arr[j++];
//        for (int p = 0; p < temp.length; p++) {
//            arr[left + p] = temp[p];
//        }        // 也可以用 System.arraycopy(a, start1, b, start2, length)    }
//    }

    public static void main(String[] args) {

        Demo demo = new Demo();
        int[] nums = {1, 3, 2, 3, 1};

        int i = demo.reversePairs(nums);

        System.out.println(i);

    }



}
